//package com.cenntro.common.kaptcha;
//
//import java.awt.image.BufferedImage;
//import java.io.ByteArrayOutputStream;
//
//import javax.annotation.Resource;
//import javax.imageio.ImageIO;
//import javax.servlet.ServletOutputStream;
//import javax.servlet.http.Cookie;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//
//import org.apache.commons.lang3.RandomStringUtils;
//import org.apache.commons.lang3.RandomUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.servlet.ModelAndView;
//
//import com.cenntro.common.redis.IRedisService;
//import com.google.code.kaptcha.impl.DefaultKaptcha;
//
//@Controller
//public class KaptchaController {
//
//    @Autowired
//    private DefaultKaptcha defaultKaptcha;
//	@Resource
//	private IRedisService redisService;
//	
//	
//    @RequestMapping("/kaptcha")
//    public String getkaptchaHtml() {
//        return "kaptcha";
//    }
//    
//    @RequestMapping("/image")
//    public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
//        byte[] captchaChallengeAsJpeg = null;
//        ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
//        try {
//            //生产验证码字符串并保存到session中
//            String createText = defaultKaptcha.createText();
//            HttpSession session =  httpServletRequest.getSession(true);
//            String key = session.getId();
//            //httpServletRequest.getSession().setAttribute("code", createText);
//			redisService.set("kaptcha_code_" + key, createText, 2 * 60);
//            Cookie cookie = new Cookie("validKey", key);  
//            cookie.setMaxAge(1 * 60);// 设置为1min  
//            cookie.setPath("/");  
//            cookie.setDomain("after.cenntroauto.cn");
////            System.out.println("已添加===============");  
//            httpServletResponse.addCookie(cookie);
//            
//            //使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
//            BufferedImage challenge = defaultKaptcha.createImage(createText);
//            ImageIO.write(challenge, "jpg", jpegOutputStream);
//        } catch (IllegalArgumentException e) {
//            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
//            return;
//        }
//
//        //定义response输出类型为image/jpeg类型，使用response输出流输出图片的byte数组
//        captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
//        httpServletResponse.setHeader("Cache-Control", "no-store");
//        httpServletResponse.setHeader("Pragma", "no-cache");
//        httpServletResponse.setDateHeader("Expires", 0);
//        httpServletResponse.setContentType("image/jpeg");
//        ServletOutputStream responseOutputStream =
//                httpServletResponse.getOutputStream();
//        responseOutputStream.write(captchaChallengeAsJpeg);
//        responseOutputStream.flush();
//        responseOutputStream.close();
//    }
//
//    @RequestMapping("/checkCode")
//    public ModelAndView checkCodeByKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
//        ModelAndView andView = new ModelAndView();
//        String captchaId = (String) httpServletRequest.getSession().getAttribute("code");
//        String parameter = httpServletRequest.getParameter("code");
//        System.out.println("Session  code " + captchaId + " form code " + parameter);
//        if (!captchaId.equals(parameter)) {
//            andView.addObject("info", "错误的验证码");
//            andView.setViewName("index");//返回的页面
//        } else {
//            andView.addObject("info", "登录成功");
//            andView.setViewName("Shiro");//返回的页面
//        }
//        return andView;
//    }
//}